<?php
/**
 * 
 * @author Radovan Dvorsky
 * @version 0.1
 *
 */
class Zend_View_Helper_DepSelect extends Zend_View_Helper_Abstract
{	
	/**
	 * Vytvara form select, kde slelect "child" je zavisly od vybranej hodnoty 
	 * v "parent"
	 * 
	 * Zavislost sa urcuje podla hodnoty value v "parent". Skupina zavislych 
	 * hodnot sa priradi pridanim class="sub_[parent option value]" do "child"
	 * 
	 * @example <code>
	 * 			<select id="parent">
	 *				<option value="1">Flower</option>
	 *				<option value="2">Animal</option>
	 *			</select>
	 *			<select id="child">
	 *				<option class="sub_1" value="1">Rose</option>
	 *				<option class="sub_1" value="2">Sunflower</option>
	 *				<option class="sub_1" value="3">Orchid</option>
	 *				<option class="sub_2" value="4">Cow</option>
	 *				<option class="sub_2" value="5">Dog</option>
	 *				<option class="sub_2" value="6">Cat</option>
	 *				<option class="sub_2" value="7">Tiger</option>
	 *			</select>
	 *			</form>
	 *			</code>	
	 * 
	 * @param string $parent
	 * @param string $child
	 * @param boolean $isSubselectOptional
	 * @param string $childVal
	 */
	public function depSelect($parent,$child,$isSubselectOptional,$childVal)
    {
        $jquery = $this->view->jQuery();
        $jquery->enable(); // enable jQuery Core Library

        // get current jQuery handler based on noConflict settings
        $jqHandler = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
		
     	$isSubselectOptional = $isSubselectOptional == true ? 'true' : 'false'; 
        
       $function = '("body").append(\'<select style="display:none" id="'. $parent . $child. '"></select>\');'
        		  . "$('#$parent$child').html($('#$child option'));"
        		  . "var parentValue = $('#$parent').attr('value');"
        		  . "$('#$child').html($('#$parent$child .sub_' + parentValue).clone());"
        		  . "childVal = (typeof childVal == \"undefined\")? \"\" : childVal ;"
        		  . "$('#$child').val(childVal).attr('selected','selected');"
        		  . "$('#$parent').change(function(){"
        		  . "var parentValue = $('#$parent').attr('value');"
        		  . "$('#$child').html($('#$parent$child .sub_' + parentValue).clone());"
        		  . "if($isSubselectOptional) $('#$child').prepend('<option value=\'none\' selected=\'selected\'> -- Select -- </option>');"
        		  . "$('#$child').trigger('change');"
        		  . "$('#$child').focus();"
        		  . "});";

        
        $jquery->addOnload($jqHandler . $function);
        return '';
    }
}